package com.softer.wxzj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.softer.wxzj.common.entity.Page;
import com.softer.wxzj.common.enums.NumStrEnum;
import com.softer.wxzj.common.enums.ShiroResultEnum;
import com.softer.wxzj.common.error.CourseException;
import com.softer.wxzj.common.util.DateUtils;
import com.softer.wxzj.common.util.OperUtil;
import com.softer.wxzj.common.util.StringUtils;
import com.softer.wxzj.entity.*;
import com.softer.wxzj.mapper.*;
import com.softer.wxzj.service.StatisticalReportFormsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Niu
 * @create 2019/11/11 - 15:33 - 星期一
 */

@Service
public class StatisticalReportFormsServiceImpl implements StatisticalReportFormsService {
    @Autowired
    private StatisticalReportFormsMapper statisticalReportFormsMapper;
    @Autowired
    private AccBusiVouchLinkMapper accBusiVouchLinkMapper;
    @Autowired
    private OperUtil zoneCodeUtil;
    @Autowired
    private AccTransactionLogMapper accTransactionLogMapper;
    @Autowired
    private ExpPayReqMapper expPayReqMapper;
    @Autowired
    private BaseContractorBankInfoMapper baseContractorBankInfoMapper;

    /**
     * 综合查询里的小区名称
     */
    @Override
    public PageInfo<StatisticalReportForms> wholeCityComprehensiveQueryRegion(Page page, String state, String fuzzy) {
        //SQL 氛围3段  拼接数据传到前端
        String zoneCode = zoneCodeUtil.getZoneCode();
        if (StringUtils.isBlank(state)) {
            zoneCode = zoneCodeUtil.getZoneCode();
        } else if (state == NumStrEnum.ONE.getNum()) {  //state==1时  查询市级
            zoneCode = zoneCode.substring(0, 2);
        }
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<StatisticalReportForms> statisticalReportFormsBasicList = statisticalReportFormsMapper.wholeCityComprehensiveQueryRegion(zoneCode, fuzzy);
        StatisticalReportForms a = statisticalReportFormsMapper.wholeCityComprehensiveQueryRegionAll(zoneCode, fuzzy);
        StatisticalReportForms aa = businessTotalAll(statisticalReportFormsBasicList);
        aa.setBuildNumber(statisticalReportFormsBasicList.stream().collect(Collectors.summingInt(StatisticalReportForms::getBuildNumber)));
        aa.setHouseNumber(statisticalReportFormsBasicList.stream().collect(Collectors.summingInt(StatisticalReportForms::getHouseNumber)));
        statisticalReportFormsBasicList.add(aa);
        statisticalReportFormsBasicList.add(a);
        PageInfo<StatisticalReportForms> pageInfo = new PageInfo<>(statisticalReportFormsBasicList);
        return pageInfo;
    }

    @Override
    public PageInfo<StatisticalReportForms> wholeCityComprehensiveQueryBuild(Page page, String regionId, String fuzzy) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<StatisticalReportForms> statisticalReportFormsBasicList = statisticalReportFormsMapper.wholeCityComprehensiveQueryBuild(regionId, fuzzy);
        for (StatisticalReportForms s : statisticalReportFormsBasicList) {
            if (StringUtils.isNotBlank(s.getSubRegionName())) {
                s.setBuildName(s.getBuildName() + "(" + s.getSubRegionName() + ")");
            }
        }
        StatisticalReportForms a = statisticalReportFormsMapper.wholeCityComprehensiveQueryBuildAll(regionId, fuzzy);
        StatisticalReportForms aa = businessTotalAll(statisticalReportFormsBasicList);
        aa.setHouseNumber(statisticalReportFormsBasicList.stream().collect(Collectors.summingInt(StatisticalReportForms::getHouseNumber)));
        aa.setStruArea(statisticalReportFormsBasicList.stream().map(StatisticalReportForms::getStruArea).reduce(BigDecimal.ZERO, BigDecimal::add));
        statisticalReportFormsBasicList.add(aa);
        statisticalReportFormsBasicList.add(a);
        PageInfo<StatisticalReportForms> pageInfo = new PageInfo<>(statisticalReportFormsBasicList);
        return pageInfo;
    }

    @Override
    public List<StatisticalReportForms> wholeCityComprehensiveQueryBuildCell(StatisticalReportForms s) {
        List<StatisticalReportForms> statisticalReportFormsBasicList = new ArrayList<>();
        if ("yes".equals(s.getThisPage())) {
            PageHelper.startPage(s.getPageNum(), s.getPageSize());
            statisticalReportFormsBasicList = statisticalReportFormsMapper.wholeCityComprehensiveQueryBuild(s.getRegionId(), s.getFuzzy());
        } else {
            s.setPageNum(0);
            s.setPageSize(0);
            statisticalReportFormsBasicList = statisticalReportFormsMapper.wholeCityComprehensiveQueryBuild(s.getRegionId(), s.getFuzzy());
        }
        for (StatisticalReportForms ss : statisticalReportFormsBasicList) {
            if (StringUtils.isNotBlank(s.getSubRegionName())) {
                ss.setBuildName(s.getBuildName() + "(" + ss.getSubRegionName() + ")");
            }
        }
        StatisticalReportForms aa = businessTotalAll(statisticalReportFormsBasicList);
        if (null != aa) {

            aa.setHouseNumber(statisticalReportFormsBasicList.stream().collect(Collectors.summingInt(StatisticalReportForms::getHouseNumber)));
            aa.setStruArea(statisticalReportFormsBasicList.stream().map(StatisticalReportForms::getStruArea).reduce(BigDecimal.ZERO, BigDecimal::add));
            aa.setBuildName("合计:");
            statisticalReportFormsBasicList.add(aa);
        }
               return statisticalReportFormsBasicList;
    }

    /**
     * 综合查询/综合查询（全）(修改)
     *
     * @param state
     * @return
     */
    @Override
    public List<StatisticalReportForms> wholeCityComprehensiveQuery(String state, String houseAttr, String houseUse) {
        //SQL 氛围3段  拼接数据传到前端
        String zoneCode = zoneCodeUtil.getZoneCode();
        //state==1时  查询市级
        if (state == NumStrEnum.ONE.getNum()) {
            zoneCode = zoneCode.substring(0, 2);
        }
        List<StatisticalReportForms> statisticalReportFormsBasicList = statisticalReportFormsMapper.wholeCityComprehensiveQueryBasic(zoneCode, houseAttr, houseUse);
        //支用信息  (修改)
        List<StatisticalReportForms> statisticalReportFormsDisburseList = statisticalReportFormsMapper.statisticalReportFormsDisburse(zoneCode, houseAttr, houseUse);
        statisticalReportFormsBasicList.get(0).setProjectSum(statisticalReportFormsDisburseList.get(0).getProjectSum());
        statisticalReportFormsBasicList.get(0).setActualAmount(statisticalReportFormsDisburseList.get(0).getActualAmount());
        //质保金
        List<StatisticalReportForms> statisticalReportFormsPremiumList = statisticalReportFormsMapper.statisticalReportFormsPremium(zoneCode, houseAttr, houseUse);
        statisticalReportFormsBasicList.get(0).setPremium(statisticalReportFormsPremiumList.get(0).getPremium());
        //质保金返还金额
        List<StatisticalReportForms> statisticalReportFormsPremiumListReturn = statisticalReportFormsMapper.statisticalReportFormsPremiumReutrn(zoneCode, houseAttr, houseUse);
        statisticalReportFormsBasicList.get(0).setPremiumReturn(statisticalReportFormsPremiumListReturn.get(0).getPremium());
        return statisticalReportFormsBasicList;
    }

    /**
     * 小区相关信息   小区交存业务信息查询
     *
     * @param page  分页
     * @param fuzzy 模糊查询的
     * @return
     */
    @Override
    public PageInfo<StatisticalReportForms> depositBusinessQuery(Page page, String fuzzy, String queryModes) {
        String zoneCode = zoneCodeUtil.getZoneCode();
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<StatisticalReportForms> list = new ArrayList<>();
        List<StatisticalReportForms> listTotal = new ArrayList<>();
        if (StringUtils.isBlank(queryModes) || ("1").equals(queryModes)) {
            list = statisticalReportFormsMapper.depositBusinessQuery(zoneCode, fuzzy);
            listTotal = statisticalReportFormsMapper.depositBusinessQueryListTotal(zoneCode, fuzzy);
        } else {
            list = statisticalReportFormsMapper.depositBusinessQueryBuild(zoneCode, fuzzy);
            for (StatisticalReportForms a : list) {
                if (StringUtils.isNotBlank(a.getSubRegionName())) {
                    a.setBuildName(a.getBuildName() + "(" + a.getSubRegionName() + ")");
                }
            }

            listTotal = statisticalReportFormsMapper.depositBusinessQueryListTotalBuild(zoneCode, fuzzy);

        }
        PageInfo<StatisticalReportForms> pageInfo = new PageInfo<>(list);
        //本页数据统计
        depositThisPage(list);
        pageInfo.getList().add(depositThisPage(list));
        //查询总共合计数据
        pageInfo.getList().addAll(listTotal);
        return pageInfo;
    }

    /**
     * @param list
     * @return
     */
    public StatisticalReportForms depositThisPage(List<StatisticalReportForms> list) {
        StatisticalReportForms statisticalReportForms = new StatisticalReportForms();
        statisticalReportForms.setDepReqSum(list.stream().collect(Collectors.summingInt(StatisticalReportForms::getDepReqSum)));
        statisticalReportForms.setTotalAmount(list.stream().map(StatisticalReportForms::getTotalAmount).reduce(BigDecimal.ZERO, BigDecimal::add));
        return statisticalReportForms;
    }

    /**
     * 小区交存业务查询==》查看
     *
     * @param page
     * @param fuzzy
     * @param no    交存编号
     * @return
     */
    @Override
    public PageInfo<StatisticalReportForms> depositDetails(Page page, String fuzzy, String no, String buildId) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<StatisticalReportForms> list = statisticalReportFormsMapper.depositDetails(fuzzy, no, buildId);
        PageInfo<StatisticalReportForms> pageInfo = new PageInfo<>(list);
        StatisticalReportForms statisticalReportForms = new StatisticalReportForms();
        statisticalReportForms.setPayAmount(list.stream().map(StatisticalReportForms::getPayAmount).reduce(BigDecimal.ZERO, BigDecimal::add));
        statisticalReportForms.setStruArea(list.stream().map(StatisticalReportForms::getStruArea).reduce(BigDecimal.ZERO, BigDecimal::add));
        statisticalReportForms.setDeferAccr(list.stream().map(StatisticalReportForms::getDeferAccr).reduce(BigDecimal.ZERO, BigDecimal::add));
        statisticalReportForms.setDueAmount(list.stream().map(StatisticalReportForms::getDueAmount).reduce(BigDecimal.ZERO, BigDecimal::add));
        pageInfo.getList().add(statisticalReportForms);
        List<StatisticalReportForms> listAll = statisticalReportFormsMapper.depositDetailsAll(fuzzy, no, buildId);
        list.add(listAll.get(0));
        return pageInfo;
    }

    /**
     * 小区欠费信息查询
     *
     * @param page
     * @param fuzzy
     * @return
     */
    @Override
    public PageInfo<StatisticalReportForms> arrearsInquiries(Page page, String fuzzy) {
        String zoneCode = zoneCodeUtil.getZoneCode();
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<StatisticalReportForms> list = statisticalReportFormsMapper.arrearsInquiries(zoneCode, fuzzy);
        PageInfo<StatisticalReportForms> pageInfo = new PageInfo<>(list);
        //本页合计查询
        if (null!=thisAllPageEntity(list)){
            pageInfo.getList().add(thisAllPageEntity(list));
        }
        //查询总共合计数据
        List<StatisticalReportForms> listTotal = statisticalReportFormsMapper.arrearsInquirieslistTotal(zoneCode, fuzzy);
       if (null!=listTotal){
           pageInfo.getList().addAll(listTotal);
       }

        return pageInfo;
    }
    /**
     * 小区欠费信息查询（分页查询数据） 的本页求和
     *
     * @param list 小区欠费信息查询（分页查询数据） 的本页求和
     * @return
     */
    public StatisticalReportForms thisAllPageEntity(List<StatisticalReportForms> list) {
        StatisticalReportForms statisticalReportForms = new StatisticalReportForms();
        statisticalReportForms.setDepReqSum(list.stream().collect(Collectors.summingInt(StatisticalReportForms::getDepReqSum)));
        statisticalReportForms.setOwnerStateSum(list.stream().collect(Collectors.summingInt(StatisticalReportForms::getOwnerStateSum)));
        statisticalReportForms.setTotalAmount(list.stream().map(StatisticalReportForms::getTotalAmount).reduce(BigDecimal.ZERO, BigDecimal::add));
        statisticalReportForms.setFunBal(list.stream().map(StatisticalReportForms::getFunBal).reduce(BigDecimal.ZERO, BigDecimal::add));
        statisticalReportForms.setFullHouseholds(list.stream().collect(Collectors.summingInt(StatisticalReportForms::getFullHouseholds)));
        statisticalReportForms.setFullDepositAmount(list.stream().map(StatisticalReportForms::getFullDepositAmount).reduce(BigDecimal.ZERO, BigDecimal::add));
        statisticalReportForms.setInsufficientHouseholds(list.stream().collect(Collectors.summingInt(StatisticalReportForms::getInsufficientHouseholds)));
        statisticalReportForms.setInsufficientDepositAmount(list.stream().map(StatisticalReportForms::getInsufficientDepositAmount).reduce(BigDecimal.ZERO, BigDecimal::add));
        statisticalReportForms.setAmountArrears(list.stream().map(StatisticalReportForms::getAmountArrears).reduce(BigDecimal.ZERO, BigDecimal::add));
        statisticalReportForms.setFullAllDepositAmount(list.stream().map(StatisticalReportForms::getFullAllDepositAmount).reduce(BigDecimal.ZERO, BigDecimal::add));
        statisticalReportForms.setFullAllHouseholds(list.stream().collect(Collectors.summingInt(StatisticalReportForms::getFullAllHouseholds)));
        return statisticalReportForms;
    }

    /**
     * 小区欠费信息详情（分页查询数据） 的本页求和
     *
     * @param list 小区欠费信息查询（分页查询数据） 的本页求和
     * @return
     */
    public StatisticalReportForms thisPageHouseEntity(List<StatisticalReportForms> list) {
        StatisticalReportForms statisticalReportForms = new StatisticalReportForms();
        statisticalReportForms.setInsufficientDepositAmount(list.stream().map(StatisticalReportForms::getInsufficientDepositAmount).reduce(BigDecimal.ZERO, BigDecimal::add));
        statisticalReportForms.setTotalAmount(list.stream().map(StatisticalReportForms::getTotalAmount).reduce(BigDecimal.ZERO, BigDecimal::add));
        statisticalReportForms.setFunBal(list.stream().map(StatisticalReportForms::getFunBal).reduce(BigDecimal.ZERO, BigDecimal::add));
        statisticalReportForms.setPayAmount(list.stream().map(StatisticalReportForms::getPayAmount).reduce(BigDecimal.ZERO, BigDecimal::add));
        statisticalReportForms.setRefundAmount(list.stream().map(StatisticalReportForms::getRefundAmount).reduce(BigDecimal.ZERO, BigDecimal::add));
        statisticalReportForms.setActualAmount(list.stream().map(StatisticalReportForms::getActualAmount).reduce(BigDecimal.ZERO, BigDecimal::add));
        return statisticalReportForms;
    }

    /**
     * 小区欠费信息详情
     *
     * @param page
     * @param fuzzy
     * @return
     */
    @Override
    public PageInfo<StatisticalReportForms> arrearsInquiriesDetails(Page page, String fuzzy, String id) {
        String zoneCode = zoneCodeUtil.getZoneCode();
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<StatisticalReportForms> list = statisticalReportFormsMapper.arrearsInquiriesDetails(zoneCode, fuzzy, id);
        PageInfo<StatisticalReportForms> pageInfo = new PageInfo<>(list);
        StatisticalReportForms a = thisPageHouseEntity(list);
        if (null != a) {
            a.setHouseSite("本页合计");
            list.add(a);
        }
        StatisticalReportForms aa = statisticalReportFormsMapper.arrearsInquiriesDetailsAll(zoneCode, fuzzy, id);
        if (null != aa) {
            aa.setHouseSite("总计");
            list.add(aa);
        }


        return pageInfo;
    }

    @Override
    public List<StatisticalReportForms> arrearsInquiriesDetailsCell(StatisticalReportForms s) {
        String zoneCode = zoneCodeUtil.getZoneCode();
        List<StatisticalReportForms> list;
        if (("yes").equals(s.getThisPage())) {
            PageHelper.startPage(s.getPageNum(), s.getPageSize());
            list = statisticalReportFormsMapper.arrearsInquiriesDetails(zoneCode, s.getFuzzy(), s.getId());
        } else {
            s.setPageSize(0);
            s.setPageNum(0);
            list = statisticalReportFormsMapper.arrearsInquiriesDetails(zoneCode, s.getFuzzy(), s.getId());
        }
        StatisticalReportForms aa = thisPageHouseEntity(list);
      if (null!=aa){
          aa.setHouseSite("合计");
          aa.setDueState("");
          aa.setState("");
          list.add(aa);
      }
        return list;
    }

    /**
     * 小区业务汇总查询
     * 小区为单位
     *
     * @param page
     * @param statisticalReportForms
     * @return
     */
    @Override
    public PageInfo<StatisticalReportForms> businessSummaryQuery(Page page, StatisticalReportForms statisticalReportForms) {
        if (StringUtils.isBlank(statisticalReportForms.getZoneCode())) {
            statisticalReportForms.setZoneCode(zoneCodeUtil.getZoneCode());
        }
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<StatisticalReportForms> list = statisticalReportFormsMapper.businessSummaryQuery(statisticalReportForms);
        PageInfo<StatisticalReportForms> pageInfo = new PageInfo<>(list);
        pageInfo.getList().add(businessTotal(list));
        List<StatisticalReportForms> listTotal = statisticalReportFormsMapper.businessSummaryQueryTotal(statisticalReportForms);
        pageInfo.getList().add(listTotal.get(0));
        return pageInfo;
    }

    /**
     * 小区业务明细查询
     * 楼栋为单位
     *
     * @param page
     * @param statisticalReportForms
     * @return
     */
    @Override
    public PageInfo<StatisticalReportForms> businessDetailsQuery(Page page, StatisticalReportForms statisticalReportForms) {
        if (StringUtils.isBlank(statisticalReportForms.getZoneCode())) {
            statisticalReportForms.setZoneCode(zoneCodeUtil.getZoneCode());
        }
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<StatisticalReportForms> list = statisticalReportFormsMapper.businessDetailsQuery(statisticalReportForms);
        for (StatisticalReportForms s : list) {
            if (StringUtils.isNotBlank(s.getSubRegionName())) {
                s.setBuildName(s.getBuildName() + "(" + s.getSubRegionName() + ")");
            }
        }
        PageInfo<StatisticalReportForms> pageInfo = new PageInfo<>(list);
        pageInfo.getList().add(businessTotal(list));
        StatisticalReportForms listTotal = statisticalReportFormsMapper.businessDetailsQueryAll(statisticalReportForms);
        pageInfo.getList().add(listTotal);
        return pageInfo;
    }

    /**
     * 小区业务明细查询
     *
     * @param page
     * @param statisticalReportForms
     * @return
     */
    @Override
    public PageInfo<StatisticalReportForms> businessDetailsSee(Page page, StatisticalReportForms statisticalReportForms) {
        if (StringUtils.isBlank(statisticalReportForms.getZoneCode())) {
            statisticalReportForms.setZoneCode(zoneCodeUtil.getZoneCode());
        }
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<StatisticalReportForms> list = statisticalReportFormsMapper.businessDetailsSee(statisticalReportForms);
        PageInfo<StatisticalReportForms> pageInfo = new PageInfo<>(list);
        pageInfo.getList().add(businessTotal(list));
        StatisticalReportForms listTotal = statisticalReportFormsMapper.businessDetailsSeeAll(statisticalReportForms);
        pageInfo.getList().add(listTotal);
        return pageInfo;
    }

    @Override
    public List<StatisticalReportForms> businessDetailsSeeCell(StatisticalReportForms statisticalReportForms) {
        if (StringUtils.isBlank(statisticalReportForms.getZoneCode())) {
            statisticalReportForms.setZoneCode(zoneCodeUtil.getZoneCode());
        }
        List<StatisticalReportForms> list;
        if (("yes").equals(statisticalReportForms.getThisPage())) {
            PageHelper.startPage(statisticalReportForms.getPageNum(), statisticalReportForms.getPageSize());
            list = statisticalReportFormsMapper.businessDetailsSee(statisticalReportForms);
        } else {
            statisticalReportForms.setPageNum(0);
            statisticalReportForms.setPageSize(0);
            list = statisticalReportFormsMapper.businessDetailsSee(statisticalReportForms);

        }
        StatisticalReportForms listTotal = businessTotal(list);
      if (listTotal!=null){
          listTotal.setHouseSite("合计:");
          listTotal.setOwnerName("");
          list.add(listTotal);
      }

        return list;
    }

    /**
     * 小区业务本页合计
     *
     * @param list
     * @return
     */
    public StatisticalReportForms businessTotal(List<StatisticalReportForms> list) {
        StatisticalReportForms statisticalReportForms = new StatisticalReportForms();
        statisticalReportForms.setCurrentAmount(list.stream().map(StatisticalReportForms::getCurrentAmount).reduce(BigDecimal.ZERO, BigDecimal::add));
        statisticalReportForms.setPayAmount(list.stream().map(StatisticalReportForms::getPayAmount).reduce(BigDecimal.ZERO, BigDecimal::add));
        statisticalReportForms.setDepositsNumber(list.stream().collect(Collectors.summingInt(StatisticalReportForms::getDepositsNumber)));
        statisticalReportForms.setInterestAmount(list.stream().map(StatisticalReportForms::getInterestAmount).reduce(BigDecimal.ZERO, BigDecimal::add));
        statisticalReportForms.setAddedIncomeQuota(list.stream().map(StatisticalReportForms::getAddedIncomeQuota).reduce(BigDecimal.ZERO, BigDecimal::add));
        statisticalReportForms.setRefundAmount(list.stream().map(StatisticalReportForms::getRefundAmount).reduce(BigDecimal.ZERO, BigDecimal::add));
        statisticalReportForms.setRefundNumber(list.stream().collect(Collectors.summingInt(StatisticalReportForms::getRefundNumber)));
        statisticalReportForms.setActualAmount(list.stream().map(StatisticalReportForms::getActualAmount).reduce(BigDecimal.ZERO, BigDecimal::add));
        statisticalReportForms.setProjectSum(list.stream().collect(Collectors.summingInt(StatisticalReportForms::getProjectSum)));
        statisticalReportForms.setInitialPayAmount(list.stream().map(StatisticalReportForms::getInitialPayAmount).reduce(BigDecimal.ZERO, BigDecimal::add));
        return statisticalReportForms;
    }


    /**
     * 银行交易汇总查询
     *
     * @param page
     * @param
     * @return
     */
    @Override
    public PageInfo<StatisticalReportForms> bankTransactionSummaryQuery(Page page, StatisticalReportForms statisticalReportForms) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<StatisticalReportForms> list = statisticalReportFormsMapper.bankTransactionSummaryQuery(statisticalReportForms);
        PageInfo<StatisticalReportForms> pageInfo = new PageInfo<>(list);
        pageInfo.getList().add(bankPageTotal(list));
        List<StatisticalReportForms> listTotal = statisticalReportFormsMapper.bankTransactionSummaryQueryTotal(statisticalReportForms);
        pageInfo.getList().addAll(listTotal);
        return pageInfo;
    }

    /**
     * 银行本页合计
     *
     * @param list
     * @return
     */
    public StatisticalReportForms bankPageTotal(List<StatisticalReportForms> list) {
        StatisticalReportForms statisticalReportForms = new StatisticalReportForms();
        statisticalReportForms.setPayAmount(list.stream().map(StatisticalReportForms::getPayAmount).reduce(BigDecimal.ZERO, BigDecimal::add));
        statisticalReportForms.setActualAmount(list.stream().map(StatisticalReportForms::getActualAmount).reduce(BigDecimal.ZERO, BigDecimal::add));
        statisticalReportForms.setRefundAmount(list.stream().map(StatisticalReportForms::getRefundAmount).reduce(BigDecimal.ZERO, BigDecimal::add));
        statisticalReportForms.setInitialPayAmount(list.stream().map(StatisticalReportForms::getInitialPayAmount).reduce(BigDecimal.ZERO, BigDecimal::add));
        return statisticalReportForms;
    }

    /**
     * 导出excel
     * exportExcelType：businessDetailsQuery ==》小区业务明细查询
     * exportExcelType：businessSummaryQuery ==》小区业务汇总查询
     *
     * @param statisticalReportForms 参数的实体
     */
    @Override
    public List<StatisticalReportForms> businessQueryExport(StatisticalReportForms statisticalReportForms) {
        if (StringUtils.isBlank(statisticalReportForms.getZoneCode())) {
            statisticalReportForms.setZoneCode(zoneCodeUtil.getZoneCode());
        }
        List<StatisticalReportForms> list = null;
        //判断需要导出查询的数据
        if ("businessDetailsQuery".equals(statisticalReportForms.getExportExcelType())) {
            //小区业务明细查询
            if (!("yes").equals(statisticalReportForms.getThisPage())) {
                statisticalReportForms.setPageNum(0);
                statisticalReportForms.setPageSize(0);
                list = statisticalReportFormsMapper.businessDetailsQuery(statisticalReportForms);
            } else {
                PageHelper.startPage(statisticalReportForms.getPageNum(), statisticalReportForms.getPageSize());
                list = statisticalReportFormsMapper.businessDetailsQuery(statisticalReportForms);
            }
        } else {
            if (!("yes").equals(statisticalReportForms.getThisPage())) {
                statisticalReportForms.setPageNum(0);
                statisticalReportForms.setPageSize(0);
                //小区业务汇总查询
                list = statisticalReportFormsMapper.businessSummaryQuery(statisticalReportForms);
            } else {
                PageHelper.startPage(statisticalReportForms.getPageNum(), statisticalReportForms.getPageSize());
                list = statisticalReportFormsMapper.businessSummaryQuery(statisticalReportForms);
            }
        }
        //合计列
        StatisticalReportForms statisticalReportFormsHeJi = businessTotal(list);
       if (null!=statisticalReportFormsHeJi){
           if ("businessDetailsQuery".equals(statisticalReportForms.getExportExcelType())) {
               statisticalReportFormsHeJi.setBuildName("合计：");
           } else {
               statisticalReportFormsHeJi.setRegionName("合计：");
           }
           list.add(statisticalReportFormsHeJi);
       }

        return list;
    }

    /**
     * 导出excel  小区欠费信息
     *
     * @return
     */
    @Override
    public List<StatisticalReportForms> arrearsInquiriesExport(StatisticalReportForms statisticalReportForms) {
        String zoneCode = zoneCodeUtil.getZoneCode();
        List<StatisticalReportForms> list = new ArrayList<>();
        if (statisticalReportForms.getThisPage().equals("yes")) {
            PageHelper.startPage(statisticalReportForms.getPageNum(), statisticalReportForms.getPageSize());
            list = statisticalReportFormsMapper.arrearsInquiries(zoneCode, statisticalReportForms.getFuzzy());
        } else {
            statisticalReportForms.setPageNum(0);
            statisticalReportForms.setPageSize(0);
            list = statisticalReportFormsMapper.arrearsInquiries(zoneCode, statisticalReportForms.getFuzzy());
        }
        StatisticalReportForms s = thisAllPageEntity(list);
        if (null!=s){
            s.setRegionName("合计");
            list.add(s);
        }

        return list;
    }

    /**
     * 小区交存业务查询  导出excel
     *
     * @return
     */
    @Override
    public List<StatisticalReportForms> depositBusinessQueryExport(StatisticalReportForms s) {
        String zoneCode = zoneCodeUtil.getZoneCode();
        List<StatisticalReportForms> list = new ArrayList<>();
        if (StringUtils.isBlank(s.getQueryModes()) || ("1").equals(s.getQueryModes())) {
            if (("yes").equals(s.getThisPage())) {
                PageHelper.startPage(s.getPageNum(), s.getPageSize());
                list = statisticalReportFormsMapper.depositBusinessQuery(zoneCode, s.getFuzzy());
            } else {
                s.setPageNum(0);
                s.setPageSize(0);
                list = statisticalReportFormsMapper.depositBusinessQuery(zoneCode, s.getFuzzy());
            }
        } else {
            if (("yes").equals(s.getThisPage())) {
                PageHelper.startPage(s.getPageNum(), s.getPageSize());
                list = statisticalReportFormsMapper.depositBusinessQueryBuild(zoneCode, s.getFuzzy());
            } else {
                s.setPageNum(0);
                s.setPageSize(0);
                list = statisticalReportFormsMapper.depositBusinessQueryBuild(zoneCode, s.getFuzzy());
            }
            for (StatisticalReportForms a : list) {
                if (StringUtils.isNotBlank(a.getSubRegionName())) {
                    a.setBuildName(a.getBuildName() + "(" + a.getSubRegionName() + ")");
                }
            }
        }
        StatisticalReportForms statisticalReportFormsHeJi = depositThisPage(list);
    if (null!=statisticalReportFormsHeJi){
        statisticalReportFormsHeJi.setRegionName("合计：");
        statisticalReportFormsHeJi.setBuildName("");
        statisticalReportFormsHeJi.setReqDate("");
        statisticalReportFormsHeJi.setHfmpDate("");
        list.add(statisticalReportFormsHeJi);
    }

        return list;
    }

    /**
     * 银行交易汇总查询 导出excel
     *
     * @param
     * @param statisticalReportForms
     * @return
     */
    @Override
    public List<StatisticalReportForms> bankTransactionSummaryQueryExport(StatisticalReportForms statisticalReportForms) {
        statisticalReportForms.setZoneCode(zoneCodeUtil.getZoneCode());
        List<StatisticalReportForms> list = new ArrayList<>();
        if (("yes").equals(statisticalReportForms.getThisPage())) {
            PageHelper.startPage(statisticalReportForms.getPageNum(), statisticalReportForms.getPageSize());
            list = statisticalReportFormsMapper.bankTransactionSummaryQuery(statisticalReportForms);
        } else {
            statisticalReportForms.setPageNum(0);
            statisticalReportForms.setPageSize(0);
            list = statisticalReportFormsMapper.bankTransactionSummaryQuery(statisticalReportForms);
        }
        StatisticalReportForms statisticalReportFormsHeJi = bankPageTotal(list);
     if (null!=statisticalReportFormsHeJi){
         statisticalReportFormsHeJi.setBankName("合计：");
         list.add(statisticalReportFormsHeJi);
     }

        return list;
    }

    @Override
    public PageInfo<StatisticalReportForms> wholeCityComprehensiveQueryHouse(Page page, String buildId, String fuzzy) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<StatisticalReportForms> statisticalReportFormsBasicList = statisticalReportFormsMapper.wholeCityComprehensiveQueryHouse(buildId, fuzzy);
        StatisticalReportForms a = statisticalReportFormsMapper.wholeCityComprehensiveQueryHouseAll(buildId, fuzzy);
        StatisticalReportForms aa = businessTotalAll(statisticalReportFormsBasicList);
        aa.setStruArea(statisticalReportFormsBasicList.stream().map(StatisticalReportForms::getStruArea).reduce(BigDecimal.ZERO, BigDecimal::add));
        statisticalReportFormsBasicList.add(aa);
        statisticalReportFormsBasicList.add(a);
        PageInfo<StatisticalReportForms> pageInfo = new PageInfo<>(statisticalReportFormsBasicList);
        return pageInfo;

    }

    @Override
    public List<StatisticalReportForms> wholeCityComprehensiveQueryHouseCell(StatisticalReportForms s) {
        List<StatisticalReportForms> statisticalReportFormsBasicList = new ArrayList<>();
        if (("yes").equals(s.getThisPage())) {
            PageHelper.startPage(s.getPageNum(), s.getPageSize());
            statisticalReportFormsBasicList = statisticalReportFormsMapper.wholeCityComprehensiveQueryHouse(s.getBuildId(), s.getFuzzy());
        } else {
            s.setPageNum(0);
            s.setPageSize(0);
            statisticalReportFormsBasicList = statisticalReportFormsMapper.wholeCityComprehensiveQueryHouse(s.getBuildId(), s.getFuzzy());
        }
        StatisticalReportForms aa = businessTotalAll(statisticalReportFormsBasicList);
      if (null!=aa){
          aa.setStruArea(statisticalReportFormsBasicList.stream().map(StatisticalReportForms::getStruArea).reduce(BigDecimal.ZERO, BigDecimal::add));
          aa.setHouseSite("合计:");
          aa.setOwnerNo("");
          aa.setOwnerName("");
          aa.setOwnerState("");
          statisticalReportFormsBasicList.add(aa);
      }

        return statisticalReportFormsBasicList;
    }

    /**
     * 综合查询本页合计
     *
     * @param list
     * @return
     */
    public StatisticalReportForms businessTotalAll(List<StatisticalReportForms> list) {
        StatisticalReportForms statisticalReportForms = new StatisticalReportForms();
        statisticalReportForms.setAddedIncomeQuota(list.stream().map(StatisticalReportForms::getAddedIncomeQuota).reduce(BigDecimal.ZERO, BigDecimal::add));
        statisticalReportForms.setRegionIncomeDistribution(list.stream().map(StatisticalReportForms::getRegionIncomeDistribution).reduce(BigDecimal.ZERO, BigDecimal::add));
        statisticalReportForms.setFullHouseholds(list.stream().collect(Collectors.summingInt(StatisticalReportForms::getFullHouseholds)));
        statisticalReportForms.setPremiumReturn(list.stream().map(StatisticalReportForms::getPremiumReturn).reduce(BigDecimal.ZERO, BigDecimal::add));
        statisticalReportForms.setInsufficientHouseholds(list.stream().collect(Collectors.summingInt(StatisticalReportForms::getInsufficientHouseholds)));
        statisticalReportForms.setUnpaidHouseholds(list.stream().collect(Collectors.summingInt(StatisticalReportForms::getUnpaidHouseholds)));
        statisticalReportForms.setInterestAmount(list.stream().map(StatisticalReportForms::getInterestAmount).reduce(BigDecimal.ZERO, BigDecimal::add));
        statisticalReportForms.setFunBal(list.stream().map(StatisticalReportForms::getFunBal).reduce(BigDecimal.ZERO, BigDecimal::add));
        statisticalReportForms.setDueAmount(list.stream().map(StatisticalReportForms::getDueAmount).reduce(BigDecimal.ZERO, BigDecimal::add));
        statisticalReportForms.setRefundAmount(list.stream().map(StatisticalReportForms::getRefundAmount).reduce(BigDecimal.ZERO, BigDecimal::add));
        statisticalReportForms.setRefundNumber(list.stream().collect(Collectors.summingInt(StatisticalReportForms::getRefundNumber)));
        statisticalReportForms.setPremium(list.stream().map(StatisticalReportForms::getPremium).reduce(BigDecimal.ZERO, BigDecimal::add));
        statisticalReportForms.setProjectSum(list.stream().collect(Collectors.summingInt(StatisticalReportForms::getProjectSum)));
        statisticalReportForms.setActualAmount(list.stream().map(StatisticalReportForms::getActualAmount).reduce(BigDecimal.ZERO, BigDecimal::add));
        return statisticalReportForms;
    }

    @Override
    public StatisticalReportForms selectStartAndEnd(String type) {
        StatisticalReportForms a = new StatisticalReportForms();
        if ("year".equals(type)) {
            String year = DateUtils.getYYYYNow();
            a.setStartTime(year + "-" + "01-" + "01");
            a.setEndTime(year + "-" + "12" + "-31");
        } else if ("month".equals(type)) {
            String month = DateUtils.getYyyyMm();
            a.setStartTime(month + "-" + "01");

            Calendar d = Calendar.getInstance();
            d.set(Calendar.DATE, 1);//把日期抄设置为当月第一天
            d.roll(Calendar.DATE, -1);//日期回滚一天，也就是最后zhidao一天
            int maxDate = d.get(Calendar.DATE);
            a.setEndTime(month + "-" + maxDate);
        } else {
            return null;
        }
        return a;
    }

    @Override
    public PageInfo<StatisticalReportForms> selectBank(Page page, StatisticalReportForms statisticalReportForms) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<StatisticalReportForms> statisticalReportFormsBasicList = statisticalReportFormsMapper.selectBank(statisticalReportForms);
        StatisticalReportForms a = statisticalReportFormsMapper.selectBankAll(statisticalReportForms);
        StatisticalReportForms aa = new StatisticalReportForms();
        aa.setDueAmount(statisticalReportFormsBasicList.stream().map(StatisticalReportForms::getDueAmount).reduce(BigDecimal.ZERO, BigDecimal::add));
        aa.setPayAmount(statisticalReportFormsBasicList.stream().map(StatisticalReportForms::getPayAmount).reduce(BigDecimal.ZERO, BigDecimal::add));
        statisticalReportFormsBasicList.add(aa);
        statisticalReportFormsBasicList.add(a);
        PageInfo<StatisticalReportForms> pageInfo = new PageInfo<>(statisticalReportFormsBasicList);
        return pageInfo;
    }

    @Override
    public List<StatisticalReportForms> selectBank(StatisticalReportForms statisticalReportForms) {
        List<StatisticalReportForms> statisticalReportFormsBasicList;

        if (("yes").equals(statisticalReportForms.getThisPage())) {
            PageHelper.startPage(statisticalReportForms.getPageNum(), statisticalReportForms.getPageSize());
            statisticalReportFormsBasicList = statisticalReportFormsMapper.selectBank(statisticalReportForms);
        } else {
            statisticalReportForms.setPageNum(0);
            statisticalReportForms.setPageSize(0);
            statisticalReportFormsBasicList = statisticalReportFormsMapper.selectBank(statisticalReportForms);
        }
        StatisticalReportForms a = new StatisticalReportForms();
        a.setDueAmount(statisticalReportFormsBasicList.stream().map(StatisticalReportForms::getDueAmount).reduce(BigDecimal.ZERO, BigDecimal::add));
        a.setPayAmount(statisticalReportFormsBasicList.stream().map(StatisticalReportForms::getPayAmount).reduce(BigDecimal.ZERO, BigDecimal::add));
        a.setBuildName("总计");
        a.setReqDate("");
        a.setBusiType("");
        a.setMoneyCome("");
        a.setNO("");
        a.setPayMethod("");
        a.setRemark("");
        a.setName("");
        statisticalReportFormsBasicList.add(a);
        return statisticalReportFormsBasicList;
    }

    @Override
    public PageInfo<StatisticalReportForms> selectBankDefinite(Page page, StatisticalReportForms statisticalReportForms) {
        List<StatisticalReportForms> list = new ArrayList<>();
        StatisticalReportForms a = new StatisticalReportForms();
        switch (statisticalReportForms.getBusiType()) {
            case "退款业务":
                PageHelper.startPage(page.getPageNum(), page.getPageSize());
                list = statisticalReportFormsMapper.selectReqByHouse(statisticalReportForms.getBusiNo(), statisticalReportForms.getFuzzy());
                a = statisticalReportFormsMapper.selectReqByHouseAll(statisticalReportForms.getBusiNo(), statisticalReportForms.getFuzzy());
                break;
            case "交存业务":
                PageHelper.startPage(page.getPageNum(), page.getPageSize());
                list = statisticalReportFormsMapper.selectDepByHouse(statisticalReportForms.getBusiNo(), statisticalReportForms.getFuzzy());
                a = statisticalReportFormsMapper.selectDepByHouseAll(statisticalReportForms.getBusiNo(), statisticalReportForms.getFuzzy());
                break;
            case "支用业务":
                PageHelper.startPage(page.getPageNum(), page.getPageSize());
                list = statisticalReportFormsMapper.selectExpByHouse(statisticalReportForms.getBusiNo(), statisticalReportForms.getFuzzy());
                a = statisticalReportFormsMapper.selectExpByHouseAll(statisticalReportForms.getBusiNo(), statisticalReportForms.getFuzzy());
                break;
        }
        PageInfo<StatisticalReportForms> pageInfo = new PageInfo<>(list);
        list.add(selectBankDefinite(list));
        list.add(a);
        return pageInfo;
    }

    @Override
    public List<StatisticalReportForms> selectBankDefiniteCell(StatisticalReportForms statisticalReportForms) {
        List<StatisticalReportForms> list = new ArrayList<>();

        switch (statisticalReportForms.getBusiType()) {
            case "退款业务":
                if (statisticalReportForms.getThisPage().equals("yes")) {
                    PageHelper.startPage(statisticalReportForms.getPageNum(), statisticalReportForms.getPageSize());
                    list = statisticalReportFormsMapper.selectReqByHouse(statisticalReportForms.getBusiNo(), statisticalReportForms.getFuzzy());
                } else {
                    statisticalReportForms.setPageSize(0);
                    statisticalReportForms.setPageNum(0);
                    list = statisticalReportFormsMapper.selectReqByHouse(statisticalReportForms.getBusiNo(), statisticalReportForms.getFuzzy());
                }
                break;
            case "交存业务":
                if (statisticalReportForms.getThisPage().equals("yes")) {
                    PageHelper.startPage(statisticalReportForms.getPageNum(), statisticalReportForms.getPageSize());
                    list = statisticalReportFormsMapper.selectDepByHouse(statisticalReportForms.getBusiNo(), statisticalReportForms.getFuzzy());
                } else {
                    statisticalReportForms.setPageSize(0);
                    statisticalReportForms.setPageNum(0);
                    list = statisticalReportFormsMapper.selectDepByHouse(statisticalReportForms.getBusiNo(), statisticalReportForms.getFuzzy());
                }
                break;
            case "使用业务":
                if (statisticalReportForms.getThisPage().equals("yes")) {
                    PageHelper.startPage(statisticalReportForms.getPageNum(), statisticalReportForms.getPageSize());
                    list = statisticalReportFormsMapper.selectExpByHouse(statisticalReportForms.getBusiNo(), statisticalReportForms.getFuzzy());
                } else {
                    statisticalReportForms.setPageSize(0);
                    statisticalReportForms.setPageNum(0);
                    list = statisticalReportFormsMapper.selectExpByHouse(statisticalReportForms.getBusiNo(), statisticalReportForms.getFuzzy());
                }
                break;
        }
        StatisticalReportForms a = selectBankDefinite(list);
        if (null!=a){
            a.setHouseSite("合计");
            a.setOwnerName("");
            list.add(a);
        }

        return list;
    }

    @Override
    public List<Integer> selectAllYear() {
        List<Integer> year = new ArrayList<>();
        Integer oldYear = accBusiVouchLinkMapper.selectMinYear();
        Integer dateNow = Integer.parseInt(DateUtils.getYYYYNow());
        if (null != oldYear && 0 != oldYear) {
            for (int i = oldYear; i <= dateNow; i++) {
                year.add(i);
            }
        } else {
            year.add(Integer.parseInt(DateUtils.getYYYYNow()));
        }
        return year;
    }

    /**
     * 总账汇总
     *
     * @param type
     * @return
     */
    @Override
    public List<StatisticalReportForms> wholebankTransaction(String type) {
        String typeName = type;
        String endName;
        String name = "本年累计";
        List<StatisticalReportForms> list = new ArrayList<>();
        List<StatisticalReportForms> lists = statisticalReportFormsMapper.selectAllLink(type);
        if (type.indexOf("-") != -1) {
            name = "本月累计";
            typeName = type + "-01 00:00:00";
        } else {
            typeName = type + "-01-01 00:00:00";
        }
        StatisticalReportForms s = new StatisticalReportForms();
        //查询上期累计金额
        BigDecimal money = accBusiVouchLinkMapper.selectByAfterYear(typeName);
        if (null == money) {
            s.setDueAmount(new BigDecimal(0));
        } else {
            s.setDueAmount(money);
        }
        s.setName("上期累计");
        //查询现在合计金额
        StatisticalReportForms ss = statisticalReportFormsMapper.selectNowAll(type);
        if (type.indexOf("-") != -1) {
            name = "本月累计";
            if (!("12").equals(type.substring(4))) {
                endName = type.substring(0, 4) + "-" + (Integer.parseInt(type.substring(5, 7)) + 1) + "-01 00:00:00";
            } else {
                endName = (Integer.parseInt(type.substring(0, 4)) + 1) + "-01-01 00:00:00";
            }
        } else {
            endName = (Integer.parseInt(type) + 1) + "-01-01 00:00:00";
        }
        //最后的专户余额
        BigDecimal m = accBusiVouchLinkMapper.selectByBeforeYear(endName);
        if (null == ss) {
            ss = new StatisticalReportForms();
        }
        if (null == m) {
            m = new BigDecimal(0);
        }
        ss.setName(name);
        ss.setDueAmount(m);
        list.add(s);
        list.addAll(lists);
        list.add(ss);
        return list;

    }

    @Override
    public List<RegionBank> selectdepAllCell(String startTime, String endTime) {
        List<AccTransactionLog> list = accTransactionLogMapper.selectByRegionAndOrg(startTime, endTime);
        List<RegionBank> regionBanks = new ArrayList<>();
        listRegionBank(list, regionBanks, startTime, endTime);
        RegionBank a = new RegionBank();
        List<BaseBankInfo> bankInfos = accTransactionLogMapper.selectByBankIdAll(startTime, endTime);
        a.setBankInfos(bankInfos);
        a.setRegionName("合计");
        regionBanks.add(a);
        return regionBanks;
    }

    private List<RegionBank> listRegionBank(List<AccTransactionLog> list, List<RegionBank> regionBanks, String startTime, String endTime) {
        for (AccTransactionLog l : list) {
            RegionBank regionBank = new RegionBank();
            if (null != l) {
                if (StringUtils.isNotBlank(l.getOperNo())) {
                    regionBank.setOrgName(l.getOperNo());
                }
                if (StringUtils.isNotBlank(l.getName())) {
                    regionBank.setRegionName(l.getName());
                }
                List<BaseBankInfo> bankInfos = new ArrayList<>();
                bankInfos = accTransactionLogMapper.selectByBankId(l.getId(), l.getOwnerId(), startTime, endTime);
                regionBank.setBankInfos(bankInfos);
            }
            regionBanks.add(regionBank);
        }
        return regionBanks;
    }

    @Override
    public List<RegionBank> selectdepAll(String startTime, String endTime) {
        List<AccTransactionLog> list = accTransactionLogMapper.selectByRegionAndOrg(startTime, endTime);
        List<RegionBank> regionBanks = new ArrayList<>();
        RegionBank a = new RegionBank();
        listRegionBank(list, regionBanks, startTime, endTime);
        List<BaseBankInfo> bankInfos = accTransactionLogMapper.selectByBankIdAll(startTime, endTime);
        a.setBankInfos(bankInfos);
        regionBanks.add(a);
        return regionBanks;
    }

    @Override
    public List<ExpPayReq> selectexpAll(String startTime, String endTime) {
        List<ExpPayReq> list = expPayReqMapper.selectExp(startTime, endTime);
        ExpPayReq e = expPayReqMapper.selectExpAll(startTime, endTime);
        list.add(e);
        return list;
    }

    private RegionBank selectOneType(String type, String typeName, String name) {
        //查询每个银行之前的数据的和1为小于   2为等于  3为小于等于
        List<BaseBankInfo> base = statisticalReportFormsMapper.selectRegionBankOn(type, typeName);
        //查询上期余额
        BigDecimal onMoney = accBusiVouchLinkMapper.selectAllByDate(typeName, type);
        RegionBank regionBankOn = new RegionBank();
        regionBankOn.setRegionName(name);
        regionBankOn.setBankInfos(base);
        regionBankOn.setPayAmount(onMoney);
        if (("1").equals(typeName)) {
            regionBankOn.setMoney(new BigDecimal(0));
            regionBankOn.setDueAmount(new BigDecimal(0));
        }

        //查询本月支出和本月合计
        if (!typeName.equals("1")) {
            BaseBankInfo baseBankInfo = accBusiVouchLinkMapper.selectOutAnIn(type, typeName);
            regionBankOn.setDueAmount(baseBankInfo.getDueAmount());
            regionBankOn.setMoney(baseBankInfo.getPayAmount());
            regionBankOn.setDate("");
            regionBankOn.setDay("");
        }
        return regionBankOn;
    }

    @Override
    public List<RegionBank> selectbankLinkAll(String type) {
        List<RegionBank> regionBanks = new ArrayList<>();
        String year = type.substring(0, 4);
        Integer month = 01;
        if (type.indexOf("-") != -1) {
            month = Integer.parseInt(type.substring(5));
        }
        RegionBank r = selectOneType(type, "1", "上年结转");

        //上年结转
        regionBanks.add(r);
        for (Integer i = month; i <= 12; i++) {
            String date = "";
            if (i < 10) {
                date = year + "-0" + i;
            } else {
                date = year + "-" + i;
            }

            //查询 该月该小区的数据
            List<AccBusiVouchLink> accs = accBusiVouchLinkMapper.selectRegionNo(date);
            for (AccBusiVouchLink acc : accs) {
                List<BaseBankInfo> bankInfos = baseContractorBankInfoMapper.getAllList();
                RegionBank regionBank = new RegionBank();
                if (7 == acc.getIsignseq()) {
                    regionBank.setRegionName("利息");
                    //若为利息则单独计算他的承办银行金额
                    List<BaseBankInfo> lists = accTransactionLogMapper.selectAllByNo(acc.getInoId());
                    regionBank.setBankInfos(lists);
                    regionBank.setDueAmount(acc.getPayAmount());
                    regionBank.setMoney(new BigDecimal(0));
                } else {
                    regionBank.setRegionName(acc.getRegionName());
                }
                regionBank.setDate(acc.getDate());
                regionBank.setDay(acc.getDay());
                //若是in则为收入否则为支出
                if (("In").equals(acc.getIoFlag())) {
                    regionBank.setDueAmount(acc.getPayAmount());
                    regionBank.setMoney(new BigDecimal(0));
                } else {
                    regionBank.setDueAmount(new BigDecimal(0));
                    regionBank.setMoney(acc.getPayAmount());
                }//账户余额
                regionBank.setPayAmount(acc.getFunbal());
                //若此银行有值则其他银行为空
                if (!("利息").equals(regionBank.getRegionName())) {
                    for (int j = 0; j < bankInfos.size(); j++) {
                        if (bankInfos.get(j).getId().equals(acc.getBankId())) {
                            bankInfos.get(j).setFunBal(acc.getPayAmount());
                        }
                    }
                }
                regionBank.setBankInfos(bankInfos);
                regionBanks.add(regionBank);
            }
            int num = accBusiVouchLinkMapper.selectByDate(date);
            if (num > 0) {
                //本月总计
                RegionBank regionBankNow = selectOneType(date, "2", "本月累计");
                //总计  查询加上今天的数据
                RegionBank regionBankAll = selectOneType(date, "3", "累计");
                regionBanks.add(regionBankNow);
                regionBanks.add(regionBankAll);
            }
        }
        return regionBanks;
    }

    /**
     * 维修资金报表打印
     *
     * @param date
     * @return
     */
    @Override
    public RegionBank wholeMoneyOutAndIn(String date) {
        String month = date.substring(0, 4);
        String year = month + "-01-01";
        RegionBank s = statisticalReportFormsMapper.wholeMoneyOutAndIn(date, year);
        BigDecimal mm = new BigDecimal(0);
        if (null == s) {
            s = new RegionBank();
            s.setPayAmount(mm);
            s.setDueAmount(mm);
            s.setPayAmountAll(mm);
            s.setPayAmountYear(mm);
            s.setPayAmountPour("0");
            s.setPayAmountAllPour("0");
            s.setExpAmount(mm);
            s.setPayAmountYear(mm);
            s.setExpAmountAll(mm);
            s.setExpAmount(mm);
            s.setMoney(mm);
            s.setMoneyAll(mm);
            s.setMoneyYear(mm);
            s.setDueAmountAll(mm);
            s.setDueAmountYear(mm);
        }
        List<BaseBankInfo> bankInfos = statisticalReportFormsMapper.selectBankByMoney(date);
        s.setBankInfos(bankInfos);
        BigDecimal bankMoneyAll = statisticalReportFormsMapper.bankMoneyAll(date);
        s.setBankMoneyAll(bankMoneyAll);
        return s;
    }

    @Override
    public List<StatisticalReportForms> wholeCityComprehensiveQueryRegionCell(StatisticalReportForms statisticalReportForms) {
        //SQL 氛围3段  拼接数据传到前端
        String zoneCode = zoneCodeUtil.getZoneCode();
        if (StringUtils.isBlank(statisticalReportForms.getState())) {
            zoneCode = zoneCodeUtil.getZoneCode();
        } else if (statisticalReportForms.getState() == NumStrEnum.ONE.getNum()) {  //state==1时  查询市级
            zoneCode = zoneCode.substring(0, 2);
        }
        String thisPage = "yes";
        List<StatisticalReportForms> list = new ArrayList<>();
        if (thisPage.equals(statisticalReportForms.getThisPage())) {
            PageHelper.startPage(statisticalReportForms.getPageNum(), statisticalReportForms.getPageSize());
            list = statisticalReportFormsMapper.wholeCityComprehensiveQueryRegion(zoneCode, statisticalReportForms.getFuzzy());
        } else {
            statisticalReportForms.setPageNum(0);
            statisticalReportForms.setPageSize(0);
            list = statisticalReportFormsMapper.wholeCityComprehensiveQueryRegion(zoneCode, statisticalReportForms.getFuzzy());
        }
        if (list.size() == 0) {
            return null;
        }
        StatisticalReportForms aa = businessTotalAll(list);
      if (null!=aa){
          aa.setBuildNumber(list.stream().collect(Collectors.summingInt(StatisticalReportForms::getBuildNumber)));
          aa.setHouseNumber(list.stream().collect(Collectors.summingInt(StatisticalReportForms::getHouseNumber)));
          aa.setRegionName("合计:");
          list.add(aa);
      }

        return list;

    }

    @Override
    public List<StatisticalReportForms> selectBankCell(StatisticalReportForms statisticalReportForms) {
        List<StatisticalReportForms> statisticalReportFormsBasicList;
        if (("yes").equals(statisticalReportForms.getThisPage())) {
            PageHelper.startPage(statisticalReportForms.getPageNum(), statisticalReportForms.getPageSize());
            statisticalReportFormsBasicList = statisticalReportFormsMapper.selectBank(statisticalReportForms);
        } else {
            statisticalReportFormsBasicList = statisticalReportFormsMapper.selectBank(statisticalReportForms);
        }
        StatisticalReportForms aa = new StatisticalReportForms();
        aa.setDueAmount(statisticalReportFormsBasicList.stream().map(StatisticalReportForms::getDueAmount).reduce(BigDecimal.ZERO, BigDecimal::add));
        aa.setPayAmount(statisticalReportFormsBasicList.stream().map(StatisticalReportForms::getPayAmount).reduce(BigDecimal.ZERO, BigDecimal::add));
        aa.setBankName("合计");
        aa.setMoneyCome("");
        aa.setRemark("");
        aa.setPayMethod("");
        aa.setNO("");
        aa.setName("");
        statisticalReportFormsBasicList.add(aa);
        return statisticalReportFormsBasicList;
    }


    /**
     * 银行本页合计--明细
     *
     * @param list
     * @return
     */
    public StatisticalReportForms selectBankDefinite(List<StatisticalReportForms> list) {
        StatisticalReportForms statisticalReportForms = new StatisticalReportForms();
        statisticalReportForms.setPayAmount(list.stream().map(StatisticalReportForms::getPayAmount).reduce(BigDecimal.ZERO, BigDecimal::add));
        statisticalReportForms.setDueAmount(list.stream().map(StatisticalReportForms::getDueAmount).reduce(BigDecimal.ZERO, BigDecimal::add));
        statisticalReportForms.setFullDepositAmount(list.stream().map(StatisticalReportForms::getFullDepositAmount).reduce(BigDecimal.ZERO, BigDecimal::add));
        statisticalReportForms.setFunBal(list.stream().map(StatisticalReportForms::getFunBal).reduce(BigDecimal.ZERO, BigDecimal::add));
        return statisticalReportForms;
    }
}
